﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.MODEL;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Strategy_Type_Mapping : System.Web.UI.Page
    {
        private IStrategyTypeBLL stratTypeCtrl = new StrategyTypeBLL();
        private CommandControl cmdFac = new CommandControl();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                tShow.ExpandAllNodes();
                LoadDataGrid();
            }

            CmdCtrl.Inserted += new EventHandler(OnInsert);
            CmdCtrl.Updated += new EventHandler(OnUpdate);
            CmdCtrl.Deleted += new EventHandler(OnDelete);
        }

        private void LoadDataGrid()
        {
            LoadDataGrid(0);
        }

        private void LoadDataGrid(int seq)
        {
            Grid.DataSource = stratTypeCtrl.Search(seq);
            Grid.DataBind();
        }
        private void OnInsert(object sender, EventArgs e)
        {
            stratTypeCtrl.Insert(txtCode.Text, txtName.Text, txtDescr.Text, "0");
        }

        private void OnUpdate(object sender, EventArgs e)
        {
            stratTypeCtrl.Update(Convert.ToInt32(hdSeq.Text), txtCode.Text, txtName.Text, txtDescr.Text, "0");
        }

        private void OnDelete(object sender, EventArgs e)
        {
            //int nRowSelected = 0;

            //if (panelGrid.Visible)
            //{
            //    foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
            //    {
            //        stratTypeCtrl.Delete(Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Sequence"]));

            //        nRowSelected++;
            //    }
            //}
            //else
            //{
            //    stratTypeCtrl.Delete(Convert.ToInt32(hdSeq.Text));
            //}
        }

        protected void Grid_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "TargetClick")
            {
                IList<StrategyTypeInfo> stratTypeList = stratTypeCtrl.Search(Convert.ToInt32(((Label)e.Item.FindControl("targetSequence")).Text), "", "", "", "0");

                hdSeq.Text = stratTypeList[0].Sequence.ToString();
                txtCode.Text = stratTypeList[0].Code;
                txtName.Text = stratTypeList[0].Name;
                txtDescr.Text = stratTypeList[0].Description;

                CmdCtrl.MapControl();
            }
        }
    }
}
